package cn.jtfadmin.core.modules.base.region.controller;



import cn.jtfadmin.base.dict.dto.DictItemModelDTO;
import cn.jtfadmin.base.lang.common.domain.dto.AntdvTreeDataRDTO;
import cn.jtfadmin.base.lang.common.domain.dto.ResultDTO;
import cn.jtfadmin.base.lang.common.domain.dto.SpecPagePDTO;
import cn.jtfadmin.base.lang.common.domain.dto.TreeRDTO;
import cn.jtfadmin.core.modules.base.region.domain.dto.BaseRegionAddPDTO;
import cn.jtfadmin.core.modules.base.region.domain.dto.BaseRegionEditPDTO;
import cn.jtfadmin.core.modules.base.region.domain.dto.BaseRegionModelDTO;
import cn.jtfadmin.core.modules.base.region.domain.entity.BaseRegionEntity;
import cn.jtfadmin.core.modules.base.region.service.BaseRegionService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 后管-区域管理 controller
 * @author jtf
 * @since 0.0.1
 */
@RestController
@AllArgsConstructor
@Validated
public class BaseRegionController {

    private BaseRegionService service;

    /**
     * 查询BaseRegionEntity树
     *
     * @return 返回树结构
     */
    @GetMapping(value = "/sys/base/region/list")
    public ResultDTO<List<TreeRDTO<BaseRegionModelDTO>>> list(SpecPagePDTO<BaseRegionEntity> specPDTO) {
        return ResultDTO.successData(service.findTree(specPDTO,BaseRegionModelDTO.class));
    }


    /**
     * 新增BaseRegionEntity
     *
     * @param addPDTO 增增参数
     * @return 无
     */
    @RequestMapping(value = "/sys/base/region/add", method = RequestMethod.POST)
    public ResultDTO<String> add(@RequestBody @Validated BaseRegionAddPDTO addPDTO) {
        service.add(addPDTO);
        return ResultDTO.success();
    }

    /**
     * 修改 BaseRegionEntity
     *
     * @param editPDTO 修改参数
     * @return 无
     */
    @PutMapping(value = "/sys/base/region/edit")
    public ResultDTO<String> edit(@RequestBody @Validated BaseRegionEditPDTO editPDTO) {
        service.edit(editPDTO);
        return ResultDTO.success();
    }


    /**
     * 按照id删除 BaseRegionEntity
     *
     * @param id id
     * @return 无
     */
    @DeleteMapping(value = "/sys/base/region/del")
    public ResultDTO<String> del(@RequestParam() @NotBlank String id) {
        service.deleteById(id);
        return ResultDTO.success();
    }

    /**
     * 批量删除 BaseRegionEntity
     *
     * @param ids 查询条件
     * @return 分页数据
     */
    @DeleteMapping(value = "/sys/base/region/delBatch")
    public ResultDTO<String> delBatch(@RequestParam() @NotBlank String ids) {
        service.deleteBatch(ids);
        return ResultDTO.success();
    }

    /**
     * 查询antdTree
     *
     * @param columns 命名列
     * @return 无
     */
    @GetMapping(value = "/sys/base/region/tree")
    public ResultDTO<List<AntdvTreeDataRDTO>> findAntdvTreeDataRDTO(String columns) {
        return ResultDTO.successData(service.findAntdTree(columns));
    }

    /**
     * 按照编号获取下级区域字典，如果为空则返回顶级
     * 字典格式 编号-名称
     * @param no 上级区域编号
     * @return
     */
    @GetMapping(value = "/common/base/region/dict")
    public ResultDTO<List<DictItemModelDTO>> findChildrenDictModel(@RequestParam(required = false) String no) {
        return ResultDTO.successData(service.findChildrenDictModel(no));
    }

    /**
     * 初始化
     *
     * @return 无
     */
    @PostMapping(value = "/sys/base/region/init")
    public ResultDTO<String> init() {
        service.init();
        return ResultDTO.success();
    }

}
